package com.wyh.springbootrabbitmq.controller;

import com.wyh.springbootrabbitmq.constant.Constant;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/producer")
@RestController
public class ProducerController {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @RequestMapping("/work")
    public String work(){
        for (int i = 0; i < 10; i++){
            String msg = "hello work~" + i;
            rabbitTemplate.convertAndSend("", Constant.WORK_QUEUE,msg);
        }
        return "消息发送成功";
    }

    @RequestMapping("/fanout")
    public String fanout(){
        for (int i = 0; i < 10; i++){
            String msg = "hello fanout~" + i;
            rabbitTemplate.convertAndSend(Constant.FANOUT_EXCHANGE, "", msg);
        }
        return "fanout：消息发送成功";
    }

    @RequestMapping("/direct")
    public String direct(){
        for (int i = 0; i < 10; i++){
            String msg = "hello direct~" + i;
            rabbitTemplate.convertAndSend(Constant.DIRECT_EXCHANGE, "a", msg);
        }
        return "direct：消息发送成功";
    }
}
